package com.hp.attendance.serlvet.history.InService;

import com.hp.attendance.entity.Department;
import com.hp.attendance.entity.Employee;
import com.hp.attendance.entity.Position;
import com.hp.attendance.service.EmployeeBysService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/showEmployee")
public class EmployeeShowServlet extends HttpServlet {
    private EmployeeBysService employeeBysService = new EmployeeBysService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            //根据employee_number获取employee对象
            String employee_number = req.getParameter("employee_number");
            Integer a;
            if(employee_number.equals("")){
                a=null;
            }else{
                a=Integer.parseInt(employee_number);
            }
            Employee employee = employeeBysService.getEmployeeByNum(a);

            //获取employee中的department_number，根据department_number获取department对象
            Integer department_number = employee.getDepartment_number();
            Department department = employeeBysService.getDepName(department_number);

            //获取employee中的position_number，根据position_number获取position对象
            Integer position_number = employee.getPosition_number();
            Position position = employeeBysService.getPosName(position_number);

            req.setAttribute("employee",employee);
            req.setAttribute("department",department);
            req.setAttribute("position",position);

            req.getRequestDispatcher("/pages/employee_detail.jsp").forward(req,resp);


        }


        catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
